package cn.highedu.coolsharkhub.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @WebFilter 表示配置过滤器 让过滤器起作用
 * filterName 表示为过滤器起一个名字
 * urlPatterns 表示要拦截的请求 /* 表示拦截所有的请求
 */
@WebFilter(filterName = "myFilter", urlPatterns = "/*")
public class MyFilter implements Filter {
    Logger logger = LoggerFactory.getLogger(MyFilter.class);
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 获取拦截的请求地址
        String requestURI = ((HttpServletRequest) servletRequest).getRequestURI();
        logger.info("过滤器前置操作");
        logger.info("拦截的请求是: {}", requestURI);
        // 放行
        filterChain.doFilter(servletRequest, servletResponse);
        logger.info("过滤器后置操作");
    }
}
